package xyz.xtt.user.service.impl;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.primitives.Longs;

import xyz.xtt.common.starter.cache.CacheManager;
import xyz.xtt.user.constants.UserCache;
import xyz.xtt.user.entity.TblUser;
import xyz.xtt.user.entity.TblUserChainAccount;
import xyz.xtt.user.mapper.TblUserChainAccountMapper;
import xyz.xtt.user.service.ITblUserChainAccountService;
import xyz.xtt.user.service.ITblUserService;
import xyz.xtt.user.vo.user.UserVo;

/**
 * <p>
 * 用户区块链账户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-23
 */
@Service
public class TblUserChainAccountServiceImpl extends ServiceImpl<TblUserChainAccountMapper, TblUserChainAccount> implements ITblUserChainAccountService {
	@Autowired
	private CacheManager cacheManager;
	@Autowired
	private ITblUserService iTblUserService;

	@Override
	public Map<Long, TblUserChainAccount> queryAfterAdd(Collection<Long> userIds) {
		LambdaQueryChainWrapper<TblUserChainAccount> query = new LambdaQueryChainWrapper<>(baseMapper);
		return query
				.in(TblUserChainAccount::getAppUserId, userIds)
				.list()
				.stream()
				.collect(toMap(TblUserChainAccount::getAppUserId, Function.identity(), (a, b) -> b));
	}

	@Override
	public TblUserChainAccount info(Long uid) {
		return new LambdaQueryChainWrapper<>(baseMapper).eq(TblUserChainAccount::getAppUserId, uid).one();
	}

	@Override
	public Map<Long, TblUserChainAccount> queryAfterAdd(Long sellUid, Long buyUid) {
		return Arrays
				.asList(sellUid, buyUid)
				.stream()
				.map(x -> cacheManager.cacheObj(() -> info(x), UserCache.TBL_USER_CHAIN_ACCOUNT, String.valueOf(x)))
				.collect(toList())
				.stream()
				.collect(toMap(TblUserChainAccount::getAppUserId, Function.identity(), (a, b) -> b));
	}

	/**
	 * 获取账户信息
	 */
	@Override
	public UserVo getAccountAddress(String uid) {
		UserVo user = new UserVo();
		user.setUid(uid);
		QueryWrapper<TblUserChainAccount> queryUserChainAccount = new QueryWrapper<>();
		queryUserChainAccount.eq("app_user_id", uid);
		TblUserChainAccount userChainAccount = baseMapper.selectOne(queryUserChainAccount, false);
		TblUser tblUser = iTblUserService.info(Longs.tryParse(uid));
		if (userChainAccount != null && !userChainAccount.getChainAccount().isEmpty()) {
			user.setAddress(userChainAccount.getChainAccount());
		}
		if (tblUser != null) {
			if (!tblUser.getNickname().isEmpty()) {
				user.setNickname(tblUser.getNickname());
			}
			if (!tblUser.getAvatarUri().isEmpty()) {
				user.setAvatar(tblUser.getAvatarUri());
			}
		}
		user.setUid(uid);
		return user;
	}
}
